import { mockProjects, mockDependencies } from '../mock/projectData'
import { request } from './request'
import { API_CONFIG } from '@/config/constants'

export const projectsApi = {
  // 获取所有项目
  getProjects() {
    return request(
      () => mockProjects,
      API_CONFIG.DELAY_TIME.PROJECTS
    )
  },

  // 获取单个项目
  getProject(id) {
    return request(
      () => mockProjects.find(p => p.id === id),
      API_CONFIG.DELAY_TIME.PROJECT
    )
  },

  // 获取依赖
  getFileDependencies(fileName) {
    // 从完整依赖数据中筛选相关节点和依赖
    const allDependencies = mockDependencies;
    
    // 找到所有与当前文件直接相关的依赖关系
    const relatedDependencies = allDependencies.dependencies.filter(dep => 
      dep.source === fileName || dep.target === fileName
    );

    // 收集所有相关节点的ID
    const relatedNodeIds = new Set();
    relatedDependencies.forEach(dep => {
      relatedNodeIds.add(dep.source);
      relatedNodeIds.add(dep.target);
    });

    // 筛选相关节点
    const relatedNodes = allDependencies.nodes.filter(node => 
      relatedNodeIds.has(node.id)
    );

    // 返回筛选后的依赖数据
    return {
      nodes: relatedNodes,
      dependencies: relatedDependencies
    };
  }
} 